export default {
    fetch: function (opt) {
        const {url, type, data = {}} = opt;
        let requestInfo = {
            method: type ? type : 'POST',
            //mode: 'cors',
            cache: 'no-cache',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            credentials: 'include'
        };
        let _URL = url;
        let _data = JSON.stringify(data || {});
        if (requestInfo.method.toLocaleLowerCase() == "post") {
            requestInfo.body = "paramsData=" + JSON.stringify(data);
        } else {
            let str = _data.replace(/[{}]/ig, "").replace(/:/ig, "=").replace(/\,/ig, "&").replace(/\"/ig, "");
            _URL = url.indexOf("?") >= 0 ? url + "&" + str : url + "?" + str;
        }

        return fetch(_URL, requestInfo)
            .then(res => res.json())
    },
    error: function (message) {
        console.error(message);
    },
    info: function (message) {
        console.info(message);
    },
}